const articleService = require('../services/articleService');
const { successResponse } = require('../utils/errorResponse');
const { formattedArticles } = require('../utils/common');

const articleController = {
  getArticleDetail: async (req, res, next) => {
    try {
      const article = await articleService.getArticleDetail(req.params.id);
      successResponse(res, article)
    } catch (error) {
      next(error);
    }
  },

  getArticleList: async (req, res, next) => {
    try {
      const { result, total } = await articleService.getArticleList(req.body);
      const formatteArticles = formattedArticles(result, 'created_at');
      successResponse(res, {
        list: formatteArticles,
        total,
      })
    } catch (error) {
      next(error);
    }
  },

  createArticle: async (req, res, next) => {
    try {
      const article = await articleService.createArticle(req.body);
      successResponse(res, article)
    } catch (error) {
      next(error);
    }
  },

  updateArticle: async (req, res, next) => {
    try {
      const articles = await articleService.updateArticle(req.body);
      successResponse(res, articles)
    } catch (error) {
      next(error);
    }
  },

  deleteArticle: async (req, res, next) => {
    try {
      const articles = await articleService.deleteArticle(req.body);
      successResponse(res, articles)
    } catch (error) {
      next(error);
    }
  },

  deleteBatchArticles: async (req, res, next) => {
    try {
      const articles = await articleService.deleteBatchArticles(req.body);
      successResponse(res, articles)
    } catch (error) {
      next(error);
    }
  }
}

module.exports = articleController